package com.ylkj.wx.controller;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ylkj.wx.constant.WeiXBaseConstant;
import com.ylkj.wx.dto.SignInfoDto;
import com.ylkj.wx.exception.WeichatException;
import com.ylkj.wx.facade.WXInfoFacade;



@Controller
@RequestMapping("/sign")
public class SignContrllor {
    
    @Resource
    private WXInfoFacade wXInfoFacade;
    
    private static final Logger LOGGER = LoggerFactory.getLogger(SignContrllor.class);
    
    private static final String nonce = "2nDgiWM7gCxhL8v0";
    
    @RequestMapping(value = "/index")
    public @ResponseBody SignInfoDto index(HttpServletRequest request) {
        //String docCode = request.getParameter("docCode");
        String jsurl = WeiXBaseConstant.WEIXIN_DOMAIN_URL + "/ylkj/wx/page/questionList.html";
        //String jsurl = "http://10.32.170.44:8080/wx/page/index";
        long timestamp = 1463540486l;//new Date().getTime();
        SignInfoDto signInfoDto = new SignInfoDto();
        String signature = "";
        try {
            try {
                LOGGER.info("js获取签名开始：");
                signature = wXInfoFacade.getSignature(timestamp, nonce, jsurl);
                LOGGER.info("js获取签名结束："+signature);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (WeichatException e) {
                e.printStackTrace();
            }
        } catch (NoSuchAlgorithmException e) {
            LOGGER.error("获取数字签名失败。");
        }
        signInfoDto.setAppId(WeiXBaseConstant.WEICHAT_APPID);
        signInfoDto.setTimeStamp(timestamp);
        signInfoDto.setNonce(nonce);
        signInfoDto.setJsurl(jsurl);
        signInfoDto.setSigNature(signature.toLowerCase());
        
        return signInfoDto;
    }

}
